package org.iteam.xlz.core.service.ticket;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.iteam.commons.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.images.Images;
import org.iteam.xlz.core.entity.ticket.TicketInfo;
public class TicketInfoService extends BaseService<TicketInfo>{
	
	static TicketInfoService instance;
	private List<Object> params=new ArrayList<Object>();
	private StringBuilder sql=null;
	
	private TicketInfoService(){}
	
	public static TicketInfoService getInstance(){
		if(null==instance){
			instance=new TicketInfoService();
		}
		return instance;
	}
	
	public boolean validateIfTicketProduct(String pid){
		try {
			return super.counts("select count(1) from ticketinfo where pid=?",pid)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public long counts(String name,String productname,String typeid,String leftTime,String rightTime){
		params.clear();
		sql=new StringBuilder("select count(1) from ticketinfo l left join product r on l.pid=r.pid left join tickettype t on l.ttid=t.ttid where 1=1");
		if(StringUtils.isNotBlank(name)){
			sql.append(" and l.infoname like ?");
			params.add("%"+name+"%");
		}
		if(StringUtils.isNotBlank(productname)){
			sql.append(" and r.productname like ?");
			params.add("%"+productname+"%");
		}
		if(StringUtils.isNotBlank(typeid)){
			sql.append(" and t.ttid= ?");
			params.add("%"+typeid+"%");
		}
		if(StringUtils.isNotBlank(leftTime)){
			sql.append(" and l.begintime > ?");
			params.add("%"+leftTime+"%");
		}
		if(StringUtils.isNotBlank(rightTime)){
			sql.append(" and l.endtime < ?");
			params.add("%"+rightTime+"%");
		}
		try {
			return super.counts(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<TicketInfo> query(String name,String productname,String typeid,String leftTime,String rightTime,long page,long rows){
		params.clear();
		sql=new StringBuilder("select l.tid,t.typename as ttid,l.infoname,l.isforever,r.productname as pid,l.datetype,"+
		"l.begintime,l.endtime,l.validdate,l.img,l.denomination,l.infointro,l.status,l.createtime,l.creator from ticketinfo "+
		"l left join product r on l.pid=r.pid left join tickettype t on l.ttid=t.ttid where 1=1");
		if(StringUtils.isNotBlank(name)){
			sql.append(" and l.infoname like ?");
			params.add("%"+name+"%");
		}
		if(StringUtils.isNotBlank(productname)){
			sql.append(" and r.productname like ?");
			params.add("%"+productname+"%");
		}
		if(StringUtils.isNotBlank(typeid)){
			sql.append(" and t.ttid= ?");
			params.add(typeid);
		}
		if(StringUtils.isNotBlank(leftTime)){
			sql.append(" and l.begintime > ?");
			params.add("%"+leftTime+"%");
		}
		if(StringUtils.isNotBlank(rightTime)){
			sql.append(" and l.endtime < ?");
			params.add("%"+rightTime+"%");
		}
		sql.append(" order by validdate asc ");
		sql.append(" limit ?,?");
		params.add((page-1)*rows);
		params.add(rows);
		try {
			return super.query(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<TicketInfo>();
		}
	}
	
	public boolean create(TicketInfo model){
		model.setTid(UUID.randomUUID().toString().replace("-","").toUpperCase());
		model.setCreatetime(DateUtil.currentTime());
		SqlBuilder builder=SqlBuilder.createInsert("ticketinfo");
		builder.addField("tid",model.getTid());
		builder.addField("ttid",model.getTtid());
		builder.addField("infoname",model.getInfoname());
		//builder.addField("isforever",model.getIsforever());
		//builder.addField("ppid",model.getPpid());
		builder.addField("begintime",model.getBegintime());
		builder.addField("endtime",model.getEndtime());
		builder.addField("datetype",1);
		//builder.addField("img",model.getImg());
		builder.addField("denomination",model.getDenomination());
		builder.addField("infointro",model.getInfointro());
		builder.addField("status",model.getStatus());
		builder.addField("createtime",model.getCreatetime());
		builder.addField("creator",model.getCreator());
		builder.addField("tickettype",model.getTickettype());
		//builder.addField("reducednumber",model.getReducednumber());
		try {
			return super.updates(builder.getSql(),builder.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(TicketInfo model){
		SqlBuilder builder=SqlBuilder.createUpdate("ticketinfo");
		builder.addField("infoname",model.getInfoname());
		//builder.addField("isforever",model.getIsforever());
		//builder.addField("ppid",model.getPpid());
		builder.addField("begintime",model.getBegintime());
		builder.addField("endtime",model.getEndtime());
		//builder.addField("img",model.getImg());
		builder.addField("denomination",model.getDenomination());
		builder.addField("infointro",model.getInfointro());
		builder.addField("status",model.getStatus());
		builder.addField("tickettype",model.getTickettype());
		//builder.addField("reducednumber",model.getReducednumber());
		builder.addWhere("tid",model.getTid());
		try {
			return super.updates(builder.getSql(),builder.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
			
	public TicketInfo findById(String id){
		try {
			return super.show("select * from ticketinfo where 1=1 and tid=?",id);
		} catch (SQLException e) {
			e.printStackTrace();
			return new TicketInfo();
		}
	}
	
	public TicketInfo findByPpid(String ppid){
		try {
			return super.show("select * from ticketinfo where 1=1 and ppid=?",ppid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new TicketInfo();
		}
	}
	
	public boolean delete(String id){
		Connection con=null;
		try {
			Images img=super.show(Images.class,"select * from images where 1=1 and objectid=?",id);
			if(null!=img){
				con=super.getConnection();
				con.setAutoCommit(false);
				int k=super.updates(con,"insert into imagesdelete(path) values(?)",img.getImgaddr());
				int j=super.updates(con,"delete from images where imgid=?",img.getImgid());
				int i=super.updates(con,"delete from ticketinfo where tid=?",id);
				if(i==1&&j==1&&k==1){
					super.commit(con);
					return true;
				}else{
					super.rollback(con);
					return false;
				}
			}else{
				return super.updates(con,"delete from ticketinfo where tid=?",id)>0;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				super.rollback(con);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
	
	public List<TicketInfo> getUnProductTicketOrAll(boolean showAll){
		try {
			String sql="select * from ticketinfo where 1=1";
			if(!showAll){
				sql+=" and ppid is null and now() between begintime and endtime and status=0";
			}
			return super.query(sql);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<TicketInfo>();
		}
	}
}
